package front;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import mt.middle;

import be.db_backend;

public class User{

	private int userID;
	private int userlvl;
	private String name;
	private String email;
	private String password;
	private Timestamp joined;
	private int numberOfPosts;
	private middle m;
	
	public User(int userID, int userlvl, String name, String email, String password, Timestamp joined, int numberOfPosts) {
		this.userID = userID;
		this.userlvl = userlvl;
		this.name = name;
		this.email = email;
		this.password = password;
		this.joined = joined;
		this.numberOfPosts = numberOfPosts;
		
		this.m = new middle(new db_backend());
	}
	
	public User(int userlvl, String name, String email, String password) {
		this.userID = 0;
		this.userlvl = userlvl;
		this.name = name;
		this.email = email;
		this.password = password;
		this.joined = new Timestamp(System.currentTimeMillis());
		this.numberOfPosts = 0;
		this.m = new middle(new db_backend());
	}
	
	public void Create(){
		try {
			int id = m.CreateUser(userID, userlvl, name, email, password, joined, numberOfPosts);
			this.userID = id;
			
		} catch (Exception e) {
			System.out.println("FAIL! INSERT!");
			e.printStackTrace();
		}
	}
	
	public void Edit(){
		System.out.println("Error, EDIT USER not yet implemented!");
	}
	public void Delete(){
		
		try {
			m.DeleteUser(userID);
		} catch (Exception e) {
			System.out.println("FAIL! DELETE!");
			e.printStackTrace();
		}
	}
	
	public synchronized void incrementNumberOfPosts(){
		m.incrementNumberOfPosts(userID);
		numberOfPosts++;
	}
	
	/**
	 * Fetches all the PMs that are send to the user with userId
	 * @return a Arraylist<PM> object, that can be null i think...
	 */
	public ArrayList<PM> getAllPMsToUser(){
		return m.getAllPMSToAUser(userID);
	} 
	
	/**
	 * Fetches all the PMs that the user with userID have sent.
	 * @return a Arraylist<PM> object, that can be null i think...
	 */
	public ArrayList<PM> getAllPMsSentByUser(){
		return m.getAllPMSFromAUser(userID);
	} 
	
	public ArrayList<Thread> getAllThreadsByUser(){
		return m.getAllThreadsByUser(userID);
	}
	
	private ArrayList<Post> getAllPostsByUser(){
		return m.getAllPostsByUser(userID);
	}
	
	public String toString(){
		return "User:"+getName() + ", " + getEmail() + ", " + getUserID();
	}
	
	public int getUserID() {
		return userID;
	}

	public void setUserID(int userID) {
		this.userID = userID;
	}

	public int getUserlvl() {
		return userlvl;
	}

	public void setUserlvl(int userlvl) {
		this.userlvl = userlvl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getJoined() {
		return joined;
	}

	public void setJoined(Timestamp joined) {
		this.joined = joined;
	}

	public int getNumberOfPosts() {
		return numberOfPosts;
	}

	public void setNumberOfPosts(int numberOfPosts) {
		this.numberOfPosts = numberOfPosts;
	}
}
